package com.geping.etl.common.service.impl;

import java.util.List;

import com.geping.etl.common.entity.Sys_DepartmentVo;
import com.geping.etl.common.util.Constants;
import com.geping.etl.utils.EnvUitl;
import com.geping.etl.utils.SpringContextUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.geping.etl.common.entity.Sys_Department;
import com.geping.etl.common.repository.Sys_DepartmentRepository;
import com.geping.etl.common.service.Sys_DepartmentService;

import javax.persistence.EntityManager;
import javax.persistence.Query;

@Service
@Transactional
public class Sys_DepartmentServiceImpl implements Sys_DepartmentService {

	@Autowired
	private Sys_DepartmentRepository sys_DepartmentRepository;

	@Autowired
	private EntityManager entityManager;

	@Override
	public List<Sys_Department> findAllSys_Department() {
		
		return sys_DepartmentRepository.findAllSysDepartment();
	}
	
	
	//根据用户名查询部门信息
	public List<Sys_Department> getSys_DepartmentByLikeDepartmentName(String departmentName){
		List<Sys_Department> departments = null;
		try {
			String querySql = "select * from SYS_DEPARTMENT WHERE is_delete = 'N' and department_name like '%"+departmentName+"%' escape ','";
			Query query = entityManager.createNativeQuery(querySql,Class.forName("com.geping.etl.common.entity.Sys_Department"));
			departments =  query.getResultList();

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
//		return sys_DepartmentRepository.getSys_DepartmentByLikeDepartmentName(departmentName);
		return departments;
	}
	
	
	//插入
	public Sys_Department save(Sys_Department sys_Department){
		
		return sys_DepartmentRepository.save(sys_Department);
	}
	
	
	//获取最大ID数
	public Integer findMaxId(){
		
		return sys_DepartmentRepository.findMaxId();
	}
	
	
	//修改部门信息
    public int updateSys_Department(Integer id_edit,String departmentName,String description, String departmentId){
    	
    	return sys_DepartmentRepository.updateSys_Department(id_edit, departmentName, description, departmentId);
    }
    
    
  //逻辑删除部门
    public int deleteSys_Department(Integer id){
    	
    	return sys_DepartmentRepository.deleteSys_Department(id);
    }

	@Override
	public int updateDepartmentId(Integer id, String departmentId) {
		return sys_DepartmentRepository.updateDepartmentID(id,departmentId);
	}

	@Override
	public Sys_Department saveDept(Sys_Department sys_Department) {
		Sys_DepartmentService departmentService= (Sys_DepartmentService) SpringContextUtil.getBean(Sys_DepartmentService.class);
		departmentService.save(sys_Department);
		if(StringUtils.isBlank(sys_Department.getDepartmentId())) {
			departmentService.updateDepartmentId(sys_Department.getId(), String.valueOf(sys_Department.getId()));
		}else{
			departmentService.updateDepartmentId(sys_Department.getId(), sys_Department.getDepartmentId()+sys_Department.getId());
		}
		return sys_Department;
	}

	@Override
	public List<Sys_Department> getSys_DepartmentByDepartmentName(String departmentName) {
		return sys_DepartmentRepository.findByDepartmentName(departmentName);
	}

	@Override
	public List<Sys_Department> getSys_DepartmentByLikeDepartmentIdAndDepartmentName(String departmentId, String departName) {
		return sys_DepartmentRepository.getSys_DepartmentByLikeDepartmentIdAndDepartmentName(departmentId, departName);
	}

	@Override
	public List<Sys_Department> getSys_DepartmentByDescriptionAndDepartmentName(String description, String departmentName) {
		return sys_DepartmentRepository.findByDescriptionAndDepartmentName(description, departmentName);
	}

	//根据描述查询部门信息
	public List<Sys_Department> getSys_DepartmentByLikeDescription(String departmentName){
		List<Sys_Department> departments = null;
		try {
			String querySql = "select * from SYS_DEPARTMENT WHERE is_delete = 'N' and description like '%"+departmentName+"%' escape ','";
			Query query = entityManager.createNativeQuery(querySql,Class.forName("com.geping.etl.common.entity.Sys_Department"));
			departments =  query.getResultList();

		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
//		return sys_DepartmentRepository.getSys_DepartmentByLikeDepartmentName(departmentName);
		return departments;
	}

	@Override
	public Sys_DepartmentVo getSysDeptVoBySysDept(Sys_Department sysDepartment) {
		Sys_DepartmentVo sys_departmentVo = new Sys_DepartmentVo();
		BeanUtils.copyProperties(sysDepartment, sys_departmentVo);
		if (Constants.TAIWAN_BANK_USER_ON.equals(EnvUitl.props.getProperty("taiwan.user"))) {
			sys_departmentVo.setDepartmentId(sys_departmentVo.getDepartmentId().substring(0, 7));
		}
		return sys_departmentVo;
	}

	@Override
	public Sys_Department getSys_DepartmentById(Integer id) {
		return sys_DepartmentRepository.findOne(id);
	}
}
